﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using Bangle.DBUtility;
using Bangle.Common;
namespace Bangle.DAL
{
    //Sys_User
    public partial class Sys_UserDAL
    {
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Bangle.Model.Sys_User UserLogin(string Account, string Password)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select UserId, Account, UserPwd, UserName, Gender, Birthday, Phone, CertificateNo, Post, Department, RoleId, CreateUserId, CreateDate, UpdateUserId, UpdateDate, Valid  ");
            strSql.Append("  from Sys_User ");
            strSql.Append(" where Valid=1 and Account=@Account and UserPwd=@UserPwd");
            SqlParameter[] parameters = {
					new SqlParameter("@Account", SqlDbType.VarChar),
                    new SqlParameter("@UserPwd", SqlDbType.VarChar)
			};
            parameters[0].Value = Account;
            parameters[1].Value = MD5.EncryptMD5(Password);


            Bangle.Model.Sys_User model = new Bangle.Model.Sys_User();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["UserId"].ToString() != "")
                {
                    model.UserId = int.Parse(ds.Tables[0].Rows[0]["UserId"].ToString());
                }
                model.Account = ds.Tables[0].Rows[0]["Account"].ToString();
                model.UserPwd = ds.Tables[0].Rows[0]["UserPwd"].ToString();
                model.UserName = ds.Tables[0].Rows[0]["UserName"].ToString();
                if (ds.Tables[0].Rows[0]["Gender"].ToString() != "")
                {
                    model.Gender = int.Parse(ds.Tables[0].Rows[0]["Gender"].ToString());
                }
                model.Birthday = ds.Tables[0].Rows[0]["Birthday"].ToString();
                model.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                model.CertificateNo = ds.Tables[0].Rows[0]["CertificateNo"].ToString();
                model.Post = ds.Tables[0].Rows[0]["Post"].ToString();
                model.Department = ds.Tables[0].Rows[0]["Department"].ToString();
                if (ds.Tables[0].Rows[0]["RoleId"].ToString() != "")
                {
                    model.RoleId = int.Parse(ds.Tables[0].Rows[0]["RoleId"].ToString());
                }
                if (ds.Tables[0].Rows[0]["CreateUserId"].ToString() != "")
                {
                    model.CreateUserId = int.Parse(ds.Tables[0].Rows[0]["CreateUserId"].ToString());
                }
                model.CreateDate = ds.Tables[0].Rows[0]["CreateDate"].ToString();
                if (ds.Tables[0].Rows[0]["UpdateUserId"].ToString() != "")
                {
                    model.UpdateUserId = int.Parse(ds.Tables[0].Rows[0]["UpdateUserId"].ToString());
                }
                model.UpdateDate = ds.Tables[0].Rows[0]["UpdateDate"].ToString();
                if (ds.Tables[0].Rows[0]["Valid"].ToString() != "")
                {
                    model.Valid = int.Parse(ds.Tables[0].Rows[0]["Valid"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }
    }
}

